package com.labs.laba_01;

import java.util.ArrayList;

public class Student {
	public static final byte NAME_LENGTH = 10;
	public static final byte SURNAME_LENGTH = 20;
	private static ArrayList<Student> CommonList = new ArrayList<Student>();
	private CharSequence Name = "\0";
	private CharSequence Surname = "\0";

	private Student(CharSequence n, CharSequence s){
		Name = n;
		Surname = s;
	}
	static public Student getNewStudent(CharSequence n, CharSequence s){
		CharSequence qn, qs;
		if(n.length()>NAME_LENGTH) qn = n.subSequence(0, NAME_LENGTH);
		else qn = n;
		if(s.length()>SURNAME_LENGTH) qs = s.subSequence(0, SURNAME_LENGTH);
		else qs = s;
		Student st = find(qn, qs);
		if(st==null){
			st = new Student(qn, qs);
			CommonList.add(st);
		}
		return st;
	}
	static public Student find(CharSequence n, CharSequence s){
		for(Student st: CommonList){
			if(st.Name.toString().toLowerCase().equals(n.toString().toLowerCase()) && st.Surname.toString().toLowerCase().equals(s.toString().toLowerCase())) return st;
		}
		return null;
	}
	public void setName(String n){
		if(n.length()>NAME_LENGTH) Name = n.substring(0, NAME_LENGTH);
		else Name = n;
	}
	public void setSurname(String s){
		if(s.length()>SURNAME_LENGTH) Surname = s.substring(0, SURNAME_LENGTH);
		else Surname = s;
	}
	public CharSequence getName(){
		return Name;
	}
	public CharSequence getSurname(){
		return Surname;
	}
	@Override
	public boolean equals(Object o) {
		Student st = (Student) o;
		if(st.Name.toString().toLowerCase().equals(this.Name.toString().toLowerCase()) && st.Surname.toString().toLowerCase().equals(this.Surname.toString().toLowerCase())) return true;
		return false;
	}
}
